package com.itheima.dao;

import com.itheima.domain.Student;

import java.util.ArrayList;

/*
学生数据操作类
增
删
改
查
操作数组
 */
public class OtherStudentDao implements BaseStudentDao {
    private  static Student[] list = new Student[999999];

   /* static {
        Student student1 = new Student("0001", "坤坤", 26, "1996-01-02");
        Student student2 = new Student("0002", "小鹿", 30, "1993-01-02");
        Student student3 = new Student("0003", "签签", 31, "1992-01-02");
        Student student4 = new Student("0004", "滔滔", 31, "1991-01-02");
        list[0] = student1;
        list[1] = student2;
        list[2] = student3;
        list[3] = student4;

    }*/

    public void addStudent(Student student) {
        // 先找空位 是null 就放下来
        for (int i = 0; i < list.length; i++) {
            Student stu = list[i];
            if (stu == null) {
                list[i] = student;
                return;
            }
        }


    }

    public ArrayList<Student> findAllStudents() {
        //想办法 把数组存入到集合中
        ArrayList<Student> stus = new ArrayList<>();

        for (int i = 0; i < list.length; i++) {
            Student student = list[i];
            if (student != null) {
                stus.add(student);
            }
        }
        return stus;
    }

    public void deleteStudent(String sid) {
        int index = getIndex(sid);
        //根据索引删除
        list[index]=null;
    }

    public int getIndex(String sid) {
        int index = -1;
        //根据学号找索引
        for (int i = 0; i < list.length; i++) {
            Student student = list[i];
            if (student!=null&&student.getSid().equals(sid)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateStudent(String sid, Student student) {
        int index = getIndex(sid);
        list[index]= student;
    }
}
